package com.tianyang.yukuaifuzs.business.trade.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.business.trade.bean.AgentWalletOutput;
import com.tianyang.yukuaifuzs.business.trade.bean.EarningsTypeOutput;
import com.tianyang.yukuaifuzs.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifuzs.common.utils.SingleCheckListener;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifuzs.utils.VerifyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：收益顶部view
 * Created by xyj on 2024/3/11
 */
public class EarningsHeaderView extends LinearLayout {

    @BindView(R.id.totalReaminTv)
    TextView totalReaminTv;
    @BindView(R.id.outAccountTotalTv)
    TextView outAccountTotalTv;
    @BindView(R.id.filterTv)
    TextView filterTv;

    private SingleCheckListener mCheckListener;

    private ICallbackListener<Integer> callbackListener;

    private List<EarningsTypeOutput> mOutput;

    private List<String> titles = new ArrayList<>();

    public EarningsHeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.layout_earnings_header, this);
        ButterKnife.bind(this);
    }

    public void setCallbackListener(ICallbackListener<Integer> callbackListener) {
        this.callbackListener = callbackListener;
    }

    @OnClick({R.id.withdrawRtv, R.id.filterTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.withdrawRtv:
                TradeOpenProvider.getInstance().openWithdrawActivity(getContext());
                break;
            case R.id.filterTv:
                if (mOutput != null && titles.size() > 0) {
                    if (mCheckListener == null) {
                        mCheckListener = new SingleCheckListener((Activity) getContext());
                    }
                    mCheckListener.handleCheck(view, filterTv, titles, new SingleCheckListener.IWheelDataChangeCallback() {
                        @Override
                        public void wheelDataChangeCallback(int position) {
                            if (mOutput != null && mOutput.size() > position && callbackListener != null) {
                                callbackListener.callback(mOutput.get(position).getService_type());
                            }
                        }
                    });
                }
                break;
        }
    }

    public void showAgentWallet(AgentWalletOutput output) {
        if (output != null) {
            totalReaminTv.setText(VerifyUtil.decimalFormat(output.getTotalReamin()));
        }
    }

    public void showAgentWallet(JSONObject output) {
        if (output != null && output.containsKey("totalAmount")) {
            outAccountTotalTv.setText(VerifyUtil.decimalFormat(output.getString("totalAmount")));
        }
    }

    public void showAgentWallet(List<EarningsTypeOutput> outputs) {
        this.mOutput = outputs;
        titles.clear();
        if (outputs != null && outputs.size() > 0) {
            filterTv.setText(VerifyUtil.checkString(outputs.get(0).getService_type_name()));
            for (EarningsTypeOutput output : outputs) {
                titles.add(output.getService_type_name());
            }
        }
    }
}
